package ar.com.spsolutions.splilabrary.view.pages.book;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import ar.com.spsolutions.splibrary.author.Author;
import ar.com.spsolutions.splibrary.book.Book;
import ar.com.spsolutions.splibrary.book.category.Category;
import ar.com.spsolutions.splibrary.services.LibraryServiceFacade;
import ar.com.spsolutions.splilabrary.view.version2.components.MainContainer;

public class BookList extends MainContainer {

	private static final long serialVersionUID = -5138292169072578126L;

	protected static final Logger LOG = Logger.getLogger(BookList.class
			.getCanonicalName());

	private List<Book> books = new ArrayList<Book>();

	private Author author;

	private String title = "";

	private String isbn = "";

	private Category category;

	private BookDetailsContainer bookDetailsContainer;

	public BookList(final List<Book> books) {
		super();
		this.books = books;
	}

	public BookList() {
		super();
	}

	@Override
	protected void addComponents() {
		final Form<BookList> form = this.getForm();
		this.addTitleFilter(form);
		this.addISBNFilter(form);
		this.addAuthorFilter(form);
		this.addCategoryFilter(form);
		this.bookDetailsContainer = new BookDetailsContainer("books");
		form.add(this.bookDetailsContainer);
		this.add(form);
		this.setOutputMarkupId(true);

	}

	public void replaceList(final AjaxRequestTarget target) {
		final BookDetailsContainer replacement = new BookDetailsContainer(
				"books", this.getBooks());
		this.bookDetailsContainer.replaceWith(replacement);
		this.bookDetailsContainer = replacement;
		target.add(this);
	}

	private void addAuthorFilter(final Form<BookList> form) {
		final ChoiceRenderer<Author> renderer = new ChoiceRenderer<Author>(
				"name");
		final PropertyModel<List<Author>> choices = new PropertyModel<List<Author>>(
				this, "authors");
		final PropertyModel<Author> model = new PropertyModel<Author>(this,
				"author");
		final DropDownChoice<Author> authorFilter = new DropDownChoice<Author>(
				"authorFilter", model, choices, renderer);

		form.add(authorFilter);
		form.add(new AjaxButton("authorFilterButton") {

			private static final long serialVersionUID = -7217673548963987042L;

			@Override
			protected void onSubmit(final AjaxRequestTarget target,
					final Form<?> form) {
				final BookList thiss = BookList.this;
				final Author tmpAuthor = thiss.getAuthor();
				if (tmpAuthor != null) {
					final LibraryServiceFacade libraryServiceFacade = thiss
							.getLibraryServiceFacade();

					thiss.setBooks(libraryServiceFacade
							.findByAuthor(libraryServiceFacade
									.attachSession(tmpAuthor)));
					thiss.replaceList(target);
				}
			}

			@Override
			protected void onError(final AjaxRequestTarget target,
					final Form<?> form) {
				// stub

			}
		});

	}

	private void addCategoryFilter(final Form<BookList> form) {
		final ChoiceRenderer<Category> renderer = new ChoiceRenderer<Category>(
				"name");
		final PropertyModel<List<Category>> choices = new PropertyModel<List<Category>>(
				this, "categories");
		final PropertyModel<Category> model = new PropertyModel<Category>(this,
				"category");
		final DropDownChoice<Category> categoryFilter = new DropDownChoice<Category>(
				"categoryFilter", model, choices, renderer);

		form.add(categoryFilter);
		form.add(new AjaxButton("categoryFilterButton") {

			private static final long serialVersionUID = -7217673548963987042L;

			@Override
			protected void onSubmit(final AjaxRequestTarget target,
					final Form<?> form) {
				final BookList thiss = BookList.this;
				final Category tmpCategory = thiss.getCategory();
				if (tmpCategory != null) {
					final LibraryServiceFacade libraryServiceFacade = thiss
							.getLibraryServiceFacade();

					thiss.setBooks(libraryServiceFacade
							.findByCategory(tmpCategory));
					thiss.replaceList(target);
				}
			}

			@Override
			protected void onError(final AjaxRequestTarget target,
					final Form<?> form) {
				// stub

			}
		});

	}

	public List<Author> getAuthors() {
		return this.getLibraryServiceFacade().findAllAuthors();
	}

	public List<Category> getCategories() {
		return this.getLibraryServiceFacade().findAllCategories();
	}

	private void addISBNFilter(final Form<BookList> form) {
		final TextField<String> isbnFilter = new TextField<String>(
				"isbnFilter", new PropertyModel<String>(this, "isbn"));

		form.add(isbnFilter);

		form.add(new AjaxButton("isbnFilterButton") {

			private static final long serialVersionUID = 3180728569840317243L;

			@Override
			protected void onSubmit(final AjaxRequestTarget target,
					final Form<?> form) {
				final BookList thiss = BookList.this;
				thiss.setBooks(thiss.getLibraryServiceFacade().findByISBNLike(
						isbnFilter.getValue()));
				thiss.replaceList(target);

			}

			@Override
			protected void onError(final AjaxRequestTarget target,
					final Form<?> form) {
				// stub
			}
		});
	}

	private void addTitleFilter(final Form<BookList> form) {
		final TextField<String> titleFilter = new TextField<String>(
				"titleFilter", new PropertyModel<String>(this, "title"));

		form.add(titleFilter);

		form.add(new AjaxLink<Void>("titleFilterButton") {

			private static final long serialVersionUID = 6457311447683166909L;

			@Override
			public void onClick(final AjaxRequestTarget target) {
				final BookList thiss = BookList.this;
				final String titleValue = titleFilter.getValue();
				thiss.setBooks(thiss.getLibraryServiceFacade().findByTitleLike(
						titleValue));
				thiss.replaceList(target);
			}

		});
	}

	private Form<BookList> getForm() {
		return new Form<BookList>("listForm");
	}

	// --accessors--

	public List<Book> getBooks() {
		return this.books;
	}

	public void setBooks(final List<Book> books) {
		this.books = books;
	}

	public Author getAuthor() {
		return this.author;
	}

	public void setAuthor(final Author author) {
		this.author = author;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	public String getIsbn() {
		return this.isbn;
	}

	public void setIsbn(final String isbn) {
		this.isbn = isbn;
	}

	public BookDetailsContainer getBookDetailsContainer() {
		return this.bookDetailsContainer;
	}

	public void setBookDetailsContainer(
			final BookDetailsContainer bookDetailsContainer) {
		this.bookDetailsContainer = bookDetailsContainer;
	}

	public Category getCategory() {
		return this.category;
	}

	public void setCategory(final Category category) {
		this.category = category;
	}

}
